﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// using Test;
using Google.Protobuf;


public enum TestEnum
{
    a,
    b,
    c
}

public class TestClass {
    public void print() {
        Debug.Log("TestClass");
    }

    public string GetTestString() {
        return "TestClass";
    }
}

public class Bootup : MonoBehaviour
{
    // Timer timer;

    private Action<Vector2> Test1 = (Vector2 dir) => {
        //  Debug.Log(dir); 
    };

    // Start is called before the first frame update
    async void Start()
    {
        // Debug.Log("Bootup Start");
        EventMgr.Instance.Init();
        // var ins = TimerMgr.Instance;
        UIConfigMgr.Instance.Init();
        UIMgr.Instance.Init();
        EffectMgr.Instance.Init();
        HttpMgr.Instance.Init();
        NetMgr.Instance.Init();
        TCPMgr.Instance.Init();
        // test.testNet.Instance.Init();
        // await TCPMgr.Instance.Connect("28un955730.oicp.vip",20272);
        await TCPMgr.Instance.Connect("127.0.0.1",3000);
        // test.testNet.Instance.MegRequest_Req(111,"test");
        // TestNetMgr.Instance.Init();
        // StartCoroutine(Test());
        // EventMgr.Instance.On<int>("Test", Test1);
        
        // // Test1 = OnDir;
        // // EventMgr.Instance.On("JoystickDir", Test1);
        // MegRequest a = new MegRequest();
        // a.Id = 1;
        // a.Name = "test";
        
        // Debug.Log(a);
        // Debug.Log((int)MegRequest.Types.Proto.Id);
        // Debug.Log(MegResponse.Types.Proto.Id);
        // // 序列化 MegRequest
        // byte[] data = a.ToByteArray();
        // Debug.Log(data.Length);
        // Debug.Log(data);

        // // 反序列化 MegRequest
        // MegRequest b = MegRequest.Parser.ParseFrom(data);
        // Debug.Log(b.Id);
        // Debug.Log(b.Name);

        // TriangleProtocol.PreLoginRequest req = new TriangleProtocol.PreLoginRequest();
        // // int id = (int)TriangleProtocol.PreLoginRequest.Types.Proto.Id;
        // Debug.Log("id:" + (int)TriangleProtocol.PreLoginRequest.Types.Proto.Id);

        // //byte sub
        // byte[] sub = new byte[4];
        // Array.Copy(data, 0, sub, 0, 4);

    }

    void OnDir(Vector2 dir)
    {
        Debug.Log("OnDir");
        Debug.Log(dir);
    }

    // Update is called once per frame
    async void Update()
    {
        // EventMgr.Instance.Fire("Test",1);
        if(Input.GetKeyDown(KeyCode.A)){
            await EffectMgr.Instance.PlayWorldEffect(1,transform.position,1f,(effect) => {
                Debug.Log("effect start");
            },(effect) => {
                Debug.Log("effect end");
            });
        }

        if(Input.GetKeyDown(KeyCode.S)){
            await EffectMgr.Instance.PlayWorldEffect(2,transform.position,1f,(effect) => {
                Debug.Log("effect start");
            },(effect) => {
                Debug.Log("effect end");
            });
        }

        if(Input.GetKeyDown(KeyCode.D)){
            // await EffectMgr.Instance.PlayWorldEffect(3);
            await EffectMgr.Instance.PlayBindEffect(2,transform,1f,(effect) => {
                Debug.Log("effect start");
            },(effect) => {
                Debug.Log("effect over");
            });
        }

        if(Input.GetKeyDown(KeyCode.Q)){
            await EffectMgr.Instance.PlayWorldEffect(4);
        }

        if(Input.GetKeyDown(KeyCode.W)){
            await EffectMgr.Instance.PlayWorldEffect(5);
        }
        if(Input.GetKeyDown(KeyCode.E)){
            // UIMgr.Instance.OpenUI("UIJoystick");
            EffectMgr.Instance.Clean();
        }

        if(Input.GetKeyDown(KeyCode.F1)){
            // TestProtocol();
            // test.testNet.Instance.MegRequest_Req(111,"test");
            UIMgr.Instance.Clean();
        }

        if(Input.GetKeyDown(KeyCode.F2)){
            // TestProtocol();
            TCPMgr.Instance.Close();
        }
    }

    void OnDestory()
    {
        UIMgr.Instance.OnDestory();
    }

    // IEnumerator Test()
    // {
    //     yield return new WaitForSeconds(1);
    //     // UIMgr.Instance.OpenUI("UIJoystick");
    //     // TestClass test = new TestClass();
    //     // UIMgr.Instance.OpenUI("UITest1",TestEnum.a);
    //     // EventMgr.Instance.Off<int>("Test",Test1);

    //     yield return new WaitForSeconds(10);
    //     EventMgr.Instance.Off("JoystickDir", Test1);
    // }

    public void TestProtocol()
    {
        // MegResponse a = new MegResponse();
        // a.Id = UnityEngine.Random.Range(1,100);
        // a.Name = "test";
        // MegResult b = new MegResult();
        // b.Msg = "test";
        // b.Code = 1;
        // a.Result1.Add(b);
        // // 序列化 MegRequest
        // byte[] data = a.ToByteArray();
        // // Debug.Log(data.Length);
        // int protoId = 1;
        // byte[] protoIdData = BitConverter.GetBytes(protoId);
        // byte[] protoData = new byte[data.Length + HttpMgr.Instance.Protocol_length];
        // Array.Copy(protoIdData, 0, protoData, 0, HttpMgr.Instance.Protocol_length);
        // Array.Copy(data, 0, protoData, HttpMgr.Instance.Protocol_length, data.Length);
        // // Debug.Log(protoData.Length);
        // HttpMgr.Instance.TestProtocol(protoData);

    }

}
